package com.local.goods.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 声明RabbitMQ的交换机和队列
 */

@Configuration
public class RabbitMQConfig {

    @Value("${rabbitmq.goods-es-exchange}")
    private String goodsESExchange;

    @Value("${rabbitmq.goods-es-update-queue}")
    private String goodsESUpdateQueue;

    @Value("${rabbitmq.goods-es-update-routing-key}")
    private String goodsESUpdateRoutingKey;

    @Value("${rabbitmq.goods-es-delete-queue}")
    private String goodsESDeleteQueue;

    @Value("${rabbitmq.goods-es-delete-routing-key}")
    private String goodsESDeleteRoutingKey;

    @Bean
    public TopicExchange goodsESExchange() {
        return new TopicExchange(this.goodsESExchange);
    }

    @Bean
    public Queue goodsESUpdateQueue() {
        return new Queue(this.goodsESUpdateQueue);
    }

    @Bean
    public Binding bindGoodsESUpdateQueue(Queue goodsESUpdateQueue, TopicExchange goodsESExchange) {
        return BindingBuilder.bind(goodsESUpdateQueue)
                .to(goodsESExchange)
                .with(this.goodsESUpdateRoutingKey);
    }

    @Bean
    public Queue goodsESDeleteQueue() {
        return new Queue(this.goodsESDeleteQueue);
    }

    @Bean
    public Binding bindGoodsESDeleteQueue(Queue goodsESDeleteQueue, TopicExchange goodsESExchange) {
        return BindingBuilder.bind(goodsESDeleteQueue)
                .to(goodsESExchange)
                .with(this.goodsESDeleteRoutingKey);
    }

}
